package com.glq1218.util;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : glq1218
 * @description :Bean拷贝工具类
 * @since : 2022/8/28 下午3:57
 */
public class BeanCopyUtils {
    public BeanCopyUtils() {
    }

    public static <T, E> T copyBean(T source, E result) {
        if (result != null && source != null) {
            try {
                BeanUtils.copyProperties(result, source);
            } catch (Exception var3) {
                throw new RuntimeException("object copy error", var3);
            }
        }

        return source;
    }

    public static <V> V copyBean(Object source, Class<V> clazz) {
        // 创建目标对象
        V result = null;
        try {
            result = clazz.getDeclaredConstructor().newInstance();
            // 实现属性拷贝
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回结果
        return result;
    }

    public static <O, V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}